package com.kuang.lock;

import java.util.concurrent.locks.ReentrantLock;

//测试可重复锁（lock版锁）：lock锁必须配对，否则会死在里面
public class Test2 {
    public static void main(String[] args) {
        Phone1 phone = new Phone1();
        new Thread(phone::sms,"A").start();
        new Thread(phone::sms,"B").start();
        /*
            Asms  结果：A获取sms锁，再获取call锁，然后释放call锁，再释放sms锁，然后B开始获取
            Acall
            Bsms
            Bcall
         */
    }
}


class Phone1{
    ReentrantLock lock = new ReentrantLock();

    public void sms(){
        lock.lock();
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"sms");
            call();//call lock锁
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
            lock.unlock();
        }
    }
    public void call(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"call");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
